package com.jdjt.dams.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.base.app.AbstractJsonController;
import com.base.common.utils.map.MapUtil;
import com.base.shiro.entity.TreeNode;
import com.base.shiro.web.validator.ResourceValidator;
import com.jdjt.dams.entity.DocCateModel;
import com.jdjt.dams.entity.DocCateRangeModel;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;


/**
 * ResourceController
 * 
 * @author xpg
 * @since 0.1
 */
public class DocCateController extends AbstractJsonController {
	public void index() {
		render("docCateList.jsp");
	}
	
	// 文件分类树
	public void docCateTree() {
		String name = getPara("name") == null ? "" : getPara("name");
		List<DocCateModel> menuList = DocCateModel.dao.getNodeList(name);
		List<TreeNode> trees = new ArrayList<TreeNode>();
		TreeNode node = null;
		TreeNode ctnode=null;
		for (DocCateModel fnode : menuList) {
			if (fnode.getInt("p_id") == 0) {				
				node = new TreeNode();
				node.setId(fnode.getInt("id"));				
				node.setField1(fnode.getStr("identity"));				
				node.setName(fnode.getStr("name"));	
				List<TreeNode> ctrees = new ArrayList<TreeNode>();
				for (DocCateModel cnode : menuList) {
					if(cnode.getInt("p_id")==fnode.getInt("id")){
						ctnode = new TreeNode();
						ctnode.setId(cnode.getInt("id"));					
						ctnode.setField1(cnode.getStr("identity"));				
						ctnode.setName(cnode.getStr("name"));
						ctrees.add(ctnode);
						node.setChildren(ctrees);
					}				
				}
				
				trees.add(node);
			}
		}
		renderJson(trees);
	}

	public void add() {
		render("docCateAdd.jsp");
	}

	public void edit() {
		setAttr("docCate", DocCateModel.dao.findById(getParaToInt()));
		render("docCateEdit.jsp");
	}

	public void view() {
		setAttr("docCate", DocCateModel.dao.findById(getParaToInt()));
		render("docCateView.jsp");
	}

	@Before(ResourceValidator.class)
	public void save() {
		getModel(DocCateModel.class).save();
		redirect("/ms/docCate");
	}

	@Before(ResourceValidator.class)
	public void update() {
		getModel(DocCateModel.class).update();
		redirect("/ms/docCate");
	}

	public void delete() {
		DocCateModel.dao.deleteById(getParaToInt());
		redirect("/ms/docCate");
	}
}
